$dir = $ARGV[0];
chomp($dir);
$outdir = $ARGV[1];
chomp($outdir);
open(WRBUFF1,">$outdir.tfam");
open(WRBUFF2,">$outdir.tped");
open(BUFF1,"../files/reference/hapmapfiles/hapmap3_noID.tfam");
while($line = <BUFF1>)
{
        chomp($line);
        @arr =split(" ",$line);
        $hash{$arr[1]} = $line;
}
for($i=1;$i<23;$i++)
{
	$ph = "chr$i"."_phased.txt";
	$buff = "BUFF$i";
	open($buff,"$dir/$ph");
	$line = <$buff>;
	
	while($line = <$buff>)
	{
		chomp($line);
		$line =~ s/\r//g;
		$line =~ s/\n//g;
		@array = split(" ",$line);
		$rsid = shift(@array);
		$pos = shift(@array);
		$line = join(" ",@array);
		print WRBUFF2 $i." ".$rsid." "."0"." ".$pos." ".$line."\n";
	}
}
open(BUFF,"$dir/chr1_phased.txt");
$line = <BUFF>;
$line =~ s/\r//g;
$line =~ s/\n//g;
chomp($line);
@array = split(" ",$line);
shift(@array);
shift(@array);
$k =1;
for($i=0;$i<@array;$i++)
{
	@arr = split('_',$array[$i]);
	if($k %2 ==0)
	{
		print WRBUFF1 $hash{$arr[0]}."\n";
	}
	$k++;
}
